package com.zwq.zwqjavanotes.utils;

//import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSON;
import com.zwq.zwqjavanotes.service.ScalingSystemIndexServiceBiz;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @Author gnplum
 * @Description //普通webservice调用工具类
 * @Date 16:21 2025/5/7
 * @return
 * @return null
 **/
@Slf4j
public class InvokeWebserviceUtil {
    /**
     * @param soapUrl
     * @param inXml
     * @return void
     * @Author gnplum
     * @Description //调用webservice接口  ScalingSystemIndexServiceBiz  示例，其他接口可参照进行修改
     * @Date 18:40 2025/5/7
     **/
    public static String invokeBySoap(String soapUrl, String inXml) throws Exception {
        String outXml = null;
        try {
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            jaxWsProxyFactoryBean.setAddress(soapUrl);
            jaxWsProxyFactoryBean.setServiceClass(ScalingSystemIndexServiceBiz.class);
            ScalingSystemIndexServiceBiz o = (ScalingSystemIndexServiceBiz) jaxWsProxyFactoryBean.create();
            outXml = o.obtainData(inXml);
        } catch (Exception e) {
            throw new Exception(">> 调用webservice接口失败：" + e.getMessage());
        }
        return outXml;
    }

    /**
     * @param wsdlUrl
     * @param methodName
     * @param args
     * @return void
     * @Author gnplum
     * @Description 利用Apache CXF框架调用webservice接口 通过JaxWsDynamicClientFactory
     * @Date 15:14 2025/5/13
     **/
    public static void invokeByJaxWsDynamicClientFactory(String wsdlUrl, String methodName, Object[] args) {
        //调用wsdl接口
        // 创建JaxWsDynamicClientFactory工厂
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        // 创建Client对象
        Client client = factory.createClient("http://localhost:8008/test.asmx?WSDL");
        // 调用方法
        Object[] results = null;
        try {
            results = client.invoke("方法名", "参数1", "参数2");
            // 处理返回结果
            if (results != null && results.length > 0) {
                System.out.println("调用结果: " + results[0]);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //输出返回结果
        System.out.println("调用结果: " + JSON.toJSONString(results));
    }

    /**
     * @param wsdlUrl
     * @param methodName
     * @param args
     * @return void
     * @Author gnplum
     * @Description 利用Apache CXF框架调用webservice接口  通过JaxWsProxyFactoryBean
     * @Date 15:26 2025/5/13
     **/
    public static void invokeByJaxWsProxyFactoryBean(String wsdlUrl, String methodName, Object[] args) {
        //调用wsdl接口
        // 创建JaxWsProxyFactoryBean工厂
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(wsdlUrl);
        factory.setServiceClass(ScalingSystemIndexServiceBiz.class);//设置接口类
        ScalingSystemIndexServiceBiz service = (ScalingSystemIndexServiceBiz) factory.create();
        // 调用方法
        //Object[] results = null;
        try {
            String result = service.obtainData("");//调用接口方法名，参数列表
            // 处理返回结果
            if (result != null) {
                System.out.println("调用结果: " + result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
